package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.WkQualityStageType;
import com.ysd.lis.entity.WkQualityTemplateRecord;
import com.ysd.lis.mapper.WkQualityStageTypeMapper;
import com.ysd.lis.request.WkQualityStageTypeRequest;
import com.ysd.lis.service.WkQualityStageTypeService;
import com.ysd.lis.service.WkQualityTemplateRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.function.Function;

/**
 * 质量控制阶段类型模板
 *
 * @author LuoXS
 * @date 2024/4/16
 * version 1.0
 */
@Service
@Slf4j
public class WkQualityStageTypeServiceImpl extends ServiceImpl<WkQualityStageTypeMapper, WkQualityStageType> implements WkQualityStageTypeService {

    @Resource
    WkQualityStageTypeMapper mapper;

    @Resource
    WkQualityTemplateRecordService templateRecordService;


    @Override
    public Result pageList(WkQualityStageTypeRequest.PageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkQualityStageType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkQualityStageType::getDelFlag, 0);
            if (StringUtils.isNotBlank(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(
                        w -> w.like(WkQualityStageType::getCode, pageParam.getSearchValue())
                                .or()
                                .like(WkQualityStageType::getName, pageParam.getSearchValue())
                );
            }
            lambdaQueryWrapper.orderByAsc(WkQualityStageType::getCreateTime);
            List<WkQualityStageType> wkCalcFormulas = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(wkCalcFormulas);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400,"查询失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result add(WkQualityStageType params) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            int insert = mapper.insert(params);
            if (insert < 0) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDit(WkQualityStageType params) {
        boolean b = this.updateById(params);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result delete(WkQualityStageType deleteParams) {
        Result result;
        try {
            int i = mapper.deleteById(deleteParams.getId());
            if (i > 0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", e.getMessage());
        }
        return result;
    }

    @Override
    public Result selectList(WkQualityStageTypeRequest.ListParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkQualityStageType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkQualityStageType::getDelFlag, 0);
            if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
                lambdaQueryWrapper.and(wrapper -> wrapper.like(WkQualityStageType::getName, pageParam.getSeachValue())
                        .or().like(WkQualityStageType::getCode, pageParam.getSeachValue()));
            }
            List<WkQualityStageType> dictList = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(dictList);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "查询失败", e.getMessage());
        }
        return result;
    }

    @Override
    public Result saveUrl(WkQualityStageType params) {
        Result result;
        try {
            WkQualityStageType source = mapper.selectById(params.getId());
            if (!source.getFileUrl().equals(params.getFileUrl())) {
                templateRecordService.add(
                        WkQualityTemplateRecord.builder()
                                .sourceFileUrl(source.getFileUrl())
                                .targetFileUrl(params.getFileUrl())
                                .sourceType("WkQualityStageType")
                                .sourceId(params.getId())
                                .build()
                );
            }

            LambdaUpdateWrapper<WkQualityStageType> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(true, WkQualityStageType::getFileUrl, params.getFileUrl());
            updateWrapper.eq(BaseEntity::getId, params.getId());
            mapper.update(null, updateWrapper);
            result = Result.succ(1, "操作成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "操作失败", e.getMessage());
        }
        return result;
    }

    private static final Function<String, Date> strToDate = x -> {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(x);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    };
}
